/****************************************************************
 * *************************README*******************************
 * This code is my GD32F330 as an example, according to the GD32 
 * official standard library to write their own library.
 * Open source in gitee.address:
 * time:2024/11/20
 * author: pilot
*****************************************************************/
#ifndef TIMER_H
#define TIMER_H

#include "gd32f3x0_libopt.h"

typedef enum
{
    TIM0,
    TIM1,
    TIM2,
    TIM13,
    TIM14,
    TIM15,
    TIM16,
}timer_index_enum;

typedef enum 
{
    /*TIMER0 CH0~CH3*/
    TIMER0_CH0         =    0x008200,    // 0x  008[A8]  2[AF2]  0[CH0] 0[TIMER0]
    TIMER0_CH1         =    0x009210,    // 0x  008[A9]  2[AF2]  1[CH1] 0[TIMER0]
    TIMER0_CH2         =    0x010220,    // 0x  010[A10] 2[AF2]  2[CH2] 0[TIMER0]
    TIMER0_CH3         =    0x011230,    // 0x  011[A11] 2[AF2]  3[CH3] 0[TIMER0]
        /**************************turn around********************************/
    TIMER0_CH0_A7_ON   =    0x007200,    // 0x  007[A7]  2[AF2]  0[CH0] 0[TIMER0]
    TIMER0_CH0_B13_ON  =    0x045200,    // 0x  045[B13] 2[AF2]  0[CH0] 0[TIMER0]

    TIMER0_CH1_B0_ON   =    0x032210,    // 0x  032[B0]  2[AF2]  1[CH1] 0[TIMER0]
    TIMER0_CH1_B14_ON  =    0x046210,    // 0x  046[B14] 2[AF2]  1[CH1] 0[TIMER0]

    TIMER0_CH2_B1_ON   =    0x033220,    // 0x  033[B1]  2[AF2]  2[CH2] 0[TIMER0]
    TIMER0_CH2_B15_ON  =    0x047220,    // 0x  047[B15] 2[AF2]  2[CH2] 0[TIMER0]

    /*TIMER1 CH0~CH3*/
    TIMER1_CH0_A5      =    0x005201,    // 0x  005[A5]  2[AF2]  0[CH0] 1[TIMER1]
    TIMER1_CH0_A15     =    0x015201,    // 0x  015[A15] 2[AF2]  0[CH0] 1[TIMER1]

    TIMER1_CH1_A1      =    0x001211,    // 0x  001[A1]  2[AF2]  1[CH1] 1[TIMER1]
    TIMER1_CH1_B3      =    0x032211,    // 0x  032[B3]  2[AF2]  1[CH1] 1[TIMER1]

    TIMER1_CH2_A2      =    0x002221,    // 0x  002[A2]  2[AF2]  2[CH2] 1[TIMER1]
    TIMER1_CH2_B10     =    0x042221,    // 0x  042[B10] 2[AF2]  2[CH2] 1[TIMER1]

    TIMER1_CH3_A3      =    0x003231,    // 0x  003[A3]  2[AF2]  3[CH3] 1[TIMER1]
    TIMER1_CH3_B11     =    0x043231,    // 0x  043[B11] 2[AF2]  3[CH3] 1[TIMER1]

    /*TIMER2 CH0~CH3*/
    TIMER2_CH0_A6      =    0x006102,    // 0x  006[A6]  1[AF1]  0[CH0] 2[TIMER2]
    TIMER2_CH0_B4      =    0x036102,    // 0x  036[B4]  1[AF1]  0[CH0] 2[TIMER2]

    TIMER2_CH1_A7      =    0x007112,    // 0x  007[A7]  1[AF1]  1[CH1] 2[TIMER2]
    TIMER2_CH1_B5      =    0x037112,    // 0x  037[B5]  1[AF1]  1[CH1] 2[TIMER2]

    TIMER2_CH2         =    0x032122,    // 0x  032[B0]  1[AF1]  2[CH2] 2[TIMER2]
    TIMER2_CH3         =    0x033132,    // 0x  033[B1]  1[AF1]  3[CH3] 2[TIMER2]

    /*TIMER13 CH0*/
    TIMER13_CH0_A4     =    0x004403,    // 0x  004[A4]  4[AF4]  0[CH0] 3[TIMER13]
    TIMER13_CH0_A7     =    0x007403,    // 0x  007[A7]  4[AF4]  0[CH0] 3[TIMER13]
    TIMER13_CH0_B1     =    0x033003,    // 0x  033[B1]  0[AF0]  0[CH0] 3[TIMER13]

    /*TIMER14 CH0 CH1*/
    TIMER14_CH0_A2     =    0x002004,    // 0x  002[A2]  0[AF0]  0[CH0] 4[TIMER14]
    TIMER14_CH0_B14    =    0x046104,    // 0x  046[B14] 1[AF1]  0[CH0] 4[TIMER14]

    TIMER14_CH1_A3     =    0x003014,    // 0x  003[A3]  0[AF0]  1[CH1] 4[TIMER14]
    TIMER14_CH1_B15    =    0x047114,    // 0x  047[B15] 1[AF1]  1[CH1] 4[TIMER14]
        /**************************turn around********************************/
    TIMER14_CH0_ON     =    0x047304,    // 0x  047[B15] 3[AF3]  0[CH0] 4[TIMER14]

    /*TIMER15 CH0*/
    TIMER15_CH0_A6     =    0x006505,    // 0x  006[A6]  5[AF5]  0[CH0] 5[TIMER15]
    TIMER15_CH0_B8     =    0x040205,    // 0x  040[B8]  2[AF2]  0[CH0] 5[TIMER15]
        /**************************turn around********************************/
    TIMER15_CH0_ON     =    0x038205,    // 0x  038[B6]  2[AF2]  0[CH0] 5[TIMER15]

    /*TIMER16 CH0*/
    TIMER16_CH0_A7     =    0x007506,    // 0x  007[A7]  5[AF5]  0[CH0] 6[TIMER16]
    TIMER16_CH0_B9     =    0x041206,    // 0x  041[B9]  2[AF2]  0[CH0] 6[TIMER16]
        /**************************turn around********************************/
    TIMER16_CH0_ON     =    0x039206,    // 0x  039[B7]  2[AF2]  0[CH0] 6[TIMER16]
}pwm_channel_enum;

#define FREQ(x) ((10800/x) - 1)

extern uint8_t Frequency;

/*****************************Internal function**************************************/
static uint32_t timer_group_get(timer_index_enum INDEX);
static uint16_t timer_channel_get(pwm_channel_enum pin);
static uint32_t IRQ_type_get(timer_index_enum INDEX);
/************************************************************************************/
void timer_interrupt_init(pwm_channel_enum pin, uint8_t pres, uint8_t priod);
void timer_pwm_init(pwm_channel_enum pin, uint8_t freq, float duty);
void pwm_fre_set(pwm_channel_enum pin, uint8_t freq);
void pwm_duty_set(pwm_channel_enum pin, float duty);

#endif
